import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/resource/gt_iconfont.dart';
import 'package:lib_uikit/theme/gt_colors_v3.dart';
import 'package:lib_uikit/widgets/gt_button_v3.dart';
import 'package:lib_uikit/widgets/gt_switch_v3.dart';
import 'package:lib_uikit/widgets/gt_textfiled_v3.dart';

///资金密码弹窗
class GTFundsPasswordDialogWidget extends StatelessWidget {
  final bool isShowClose;
  final String title;
  final String? contentText;
  final String leftText;
  final String rightText;
  final String? switchTips;
  final String? bottomTips;
  final GestureTapCallback? onLeftTap;
  final GestureTapCallback? onRightTap;
  final bool? showVerifyPassword;

  const GTFundsPasswordDialogWidget({
    Key? key,
    this.contentText,
    this.title = '',
    this.isShowClose = false,
    this.leftText = '',
    this.rightText = '',
    this.onLeftTap,
    this.onRightTap,
    this.showVerifyPassword = false,
    this.switchTips = '',
    this.bottomTips = '',
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: Center(
        child: Container(
          margin: const EdgeInsets.symmetric(horizontal: 24),
          decoration: BoxDecoration(
            color: GTColorsV3.cmpt16.color,
            borderRadius: BorderRadius.circular(8),
          ),
          child: _renderContains(context),
        ),
      ),
    );
  }

  Widget _renderContains(BuildContext context) {
    if (isShowClose) {
      return Stack(
        children: [
          _renderContentItem(context),
          Positioned(
            top: 8,
            right: 8,
            child: GestureDetector(
              child: Padding(
                padding: const EdgeInsets.all(8),
                child: Icon(
                  GTIconFont.e89b,
                  size: 16,
                  color: GTColorsV3.icon3.color,
                ),
              ),
              onTap: () {
                if (Navigator.canPop(context)) {
                  Navigator.of(context).pop();
                }
              },
            ),
          ),
        ],
      );
    }
    return _renderContentItem(context);
  }

  Widget _renderContentItem(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(24.0),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Padding(
            padding: EdgeInsets.only(right: isShowClose ? 20 : 0, bottom: 16),
            child: Text(
              title,
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.w500,
                color: GTColorsV3.text1.color,
              ),
            ),
          ),
          if (showVerifyPassword ?? false) ...[
            Column(
              children: [
                _switchTips(),
                const SizedBox(
                  height: 16,
                ),
                GTTextFieldV3(
                  textFieldType: GTTextFieldTypeV3.stroke,
                ),
                const SizedBox(
                  height: 16,
                ),
                GTTextFieldV3(
                  textFieldType: GTTextFieldTypeV3.stroke,

                ),
                const SizedBox(
                  height: 8,
                ),
                Text(
                  bottomTips ?? "",
                  style: gtTextStyleW400(fontSize: 12, color: GTColorsV3.text5.color),
                ),
              ],
            )
          ],
          if ((contentText ?? "").isNotEmpty) ...[
            Text(
              contentText ?? "",
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.w400,
                color: GTColorsV3.text2.color,
              ),
            ),
            const SizedBox(height: 16),
          ],
          const SizedBox(height: 8),
          _renderButton(context)
        ],
      ),
    );
  }

  Widget _switchTips() {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
      decoration: BoxDecoration(color: GTColorsV3.bg2.color, borderRadius: BorderRadius.circular(4)),
      child: Row(
        mainAxisSize: MainAxisSize.max, // 设置为max，以便Row占据尽可能多的水平空间
        children: [
          Expanded(
            // 使用Expanded来使Text填充剩余空间
            child: Text(
              switchTips ?? "",
              style: gtTextStyleW400(fontSize: 12, color: GTColorsV3.text1.color),
            ),
          ),
          SizedBox(
            width: 8,
          ),
          GTSwitchV3(),
        ],
      ),
    );
  }

  Widget _renderButton(BuildContext context) {
    if (leftText.isEmpty) {
      return _renderButtonItem(context, rightText, GTButtonTypeV3.primary, onRightTap);
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Expanded(
          child: _renderButtonItem(context, leftText, GTButtonTypeV3.secondary, onLeftTap),
        ),
        const SizedBox(width: 8),
        Expanded(
          child: _renderButtonItem(context, rightText, GTButtonTypeV3.primary, onRightTap),
        )
      ],
    );
  }

  Widget _renderButtonItem(BuildContext context, String title, GTButtonTypeV3 buttonType, GestureTapCallback? onTap) {
    return GTButtonV3(
        text: title,
        buttonSize: GTButtonSizeV3.medium,
        buttonType: buttonType,
        isFullWidth: true,
        onPressed: () {
          if (Navigator.canPop(context)) {
            Navigator.of(context).pop();
          }
          onTap?.call();
        });
  }
}
